﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import Classes.*;
    import Classes.actions.*;
    import com.zynga.skelly.util.*;

    public class CopNPCActionSelection implements IActionSelection {

        protected static var WANDER_TIME:Number = 10;
        protected static var PAUSE_TIME:Number = 3;

        protected var m_npc:NPC;
        protected var m_target:MapResource;
        protected var m_arrived:Boolean = false;

        public function CopNPCActionSelection(_arg1:NPC, _arg2:MapResource=null){
            this.m_npc = _arg1;
            this.m_target = _arg2;
        }
        public static function fallBackBeeline(_arg1:NPC, _arg2:MapResource, _arg3:CopNPCActionSelection):void{
            var npc:* = _arg1;
            var target:* = _arg2;
            var selection:* = _arg3;
            var connected:* = Global.world.citySim.roadManager.getConnectedRoadGraph(target);
            if (!connected){
                npc.getStateMachine().addActions(new ActionFn(npc, function ():void{
                    npc.setPosition(target.getHotspot().x, target.getHotspot().y);
                    selection.arrived = true;
                }));
            } else {
                npc.getStateMachine().addActions(new ActionNavigateBeeline(npc, Global.world.citySim.roadManager.findClosestWalkableTileInGraph(npc.getPosition(), connected).getPosition(), null));
            };
        }

        public function get target():MapResource{
            return (this.m_target);
        }
        public function set target(_arg1:MapResource):void{
            this.arrived = ((_arg1)!=this.m_target) ? false : this.arrived;
            this.m_target = _arg1;
            this.m_npc.clearStates();
        }
        public function get arrived():Boolean{
            return (this.m_arrived);
        }
        public function set arrived(_arg1:Boolean):void{
            this.m_arrived = _arg1;
        }
        public function getNextActions():Array{
            var _local1:SelectionResult = SelectionResult.FAIL;
            _local1 = this.goToTarget();
            if (_local1 == SelectionResult.FAIL){
                _local1 = this.patrolTarget();
            };
            if (_local1 == SelectionResult.FAIL){
                this.arrived = false;
                this.m_npc.animation = "static";
                return ([new ActionNavigateWander(this.m_npc, null).setIdleTime(3)]);
            };
            return (_local1.actions);
        }
        protected function goToTarget():SelectionResult{
            if (((((!(this.target)) || (!(Global.world.getObjectById(this.target.getId()))))) || (this.arrived))){
                return (SelectionResult.FAIL);
            };
            var curState:* = this.m_npc.getStateMachine().getState();
            if ((curState is ActionNavigate)){
                if ((curState as ActionNavigate).target == this.target){
                    return (SelectionResult.FAIL);
                };
            };
            return (new SelectionResult(this.target, [new ActionNavigate(this.m_npc, this.target, null).setFallbackAction(new ActionFn(this.m_npc, Curry.curry(CopNPCActionSelection.fallBackBeeline, this.m_npc, this.target, this))), new ActionFn(this.m_npc, Curry.curry(function (_arg1:CopNPCActionSelection):void{
                _arg1.arrived = true;
            }, this))]));
        }
        protected function patrolTarget():SelectionResult{
            if (((!(this.target)) || (!(Global.world.getObjectById(this.target.getId()))))){
                return (SelectionResult.FAIL);
            };
            return (new SelectionResult(this.target, [new ActionBusinessPacing(this.m_npc, this.target, false).setCheckBusiness(true)]));
        }

    }
}//package Classes.sim 
